<template>
	<div class="shopcart">
		<div class="content" @click="toggleList">
			<div class="content-left">
				<div class="logo-wraper">
					<div class="logo" :class="{'highlight':totalCount>0}">
						<span class="icon-shopping_cart" :class="{'highlight':totalCount>0}"></span>
					</div>
					<div v-show="totalCount>0" class="num">
						{{totalCount}}
					</div>
				</div>
				<div class="price" :class="{'highlight':totalCount>0}">
					¥{{totalPrice}}
				</div>
				<div class="desc">
					另需配送费¥{{deliveryPrice}}元
				</div>
			</div>
			<div class="content-right" @click.stop.prevent="pay">
				<div class="pay" :class="payClass">
					{{payDesc}}
				</div>
			</div>
		</div>
		<div class="ball-container">
			<div v-for="ball in balls">
				<transition name="drop" v-on:before-enter="dropBefore" v-on:enter="droping" v-on:after-leave="dropAfter">
					<div class="ball" v-show="ball.show">
						<div class="inner inner-hook"></div>
					</div>
				</transition>
			</div>
		</div>
		<transition name="fold">
			<div class="shopcart-list" v-show="listShow">
				<div class="list-header">
					<h1 class="title">购物车</h1>
					<span class="empty" v-on:click="empty">清空</span>
				</div>
				<div class="list-content" ref="listContent">
					<ul>
						<li class="food" v-for="food in selectFoods">
							<span class="name">{{food.name}}</span>
							<div class="price">
								<span>¥{{food.price*food.count}}</span>
							</div>
							<div class="cartcontrol-wraper">
								<cartcontrol :food="food"></cartcontrol>
							</div>
						</li>
					</ul>
				</div>
			</div>
		</transition>
		<transition name="mask">
			<div v-show="listShow" v-on:click="hideList" class="list-mask">
			</div>
		</transition>
	</div>
</template>

<script type="text/ecmascript-6">
	import BScroll from 'better-scroll';
	import cartcontrol from '../cartcontrol/cartcontrol.vue';

	export default {
		props: {
			selectFoods: {
				type: Array,
				default() {
					return [];
				}
			},
			deliveryPrice: {
				type: Number,
				default: 0
			},
			minPrice: {
				type: Number,
				default: 0
			}
		},
		data() {
			return {
				balls: [{
					id: 0,
					show: false
				}, {
					id: 1,
					show: false
				}, {
					id: 2,
					show: false
				}, {
					id: 3,
					show: false
				}, {
					id: 4,
					show: false
				}],
				dropBall: [],
				fold: true
			};
		},
		computed: {
			listShow() {
				if(!this.totalCount) {
					this.fold = true;
					return false;
				}
				let show = !this.fold;
				if(show) {
					this.$nextTick(() => {
						if(!this.scroll) {
							this.scroll = new BScroll(this.$refs.listContent, {
								click: true
							});
						} else {
							this.scroll.refresh();
						}
					});
				}
				return show;
			},
			totalPrice() {
				let total = 0;
				this.selectFoods.forEach((food) => {
					total += food.price * food.count;
				});
				return total;
			},
			totalCount() {
				let count = 0;
				this.selectFoods.forEach((food) => {
					count += food.count;
				});
				return count;
			},
			payDesc() {
				if(this.totalPrice === 0) {
					return `¥${this.totalPrice}元起送`;
				} else if(this.totalPrice < this.minPrice) {
					let diff = this.minPrice - this.totalPrice;
					return `还差¥${diff}元起送`;
				} else {
					return '去结算';
				}
			},
			payClass() {
				if(this.totalPrice < this.minPrice) {
					return 'not-enough';
				} else {
					return 'enough';
				}
			}
		},
		methods: {
			pay() {
				if(this.totalPrice < this.minPrice) {
					return;
				}
				window.alert(`支付${this.totalPrice}元`);
			},
			hideList() {
				this.fold = true;
			},
			toggleList() {
				if(!this.totalCount) {
					return;
				}
				this.fold = !this.fold;
			},
			empty() {
				this.selectFoods.forEach((food) => {
					food.count = 0;
				});
			},
			drop(el) {
				for(let i = 0; i < this.balls.length; i++) {
					let ball = this.balls[i];
					if(!ball.show) {
						ball.show = true;
						ball.el = el;
						this.dropBall.push(ball);
						return;
					}
				}
			},
			dropBefore(el) {
				let count = this.balls.length;
				while(count--) {
					let ball = this.balls[count];
					if(ball.show) {
						let rect = ball.el.getBoundingClientRect();
						let x = rect.left - 32;
						let y = -(window.innerHeight - rect.top - 22);
						el.style.display = '';
						console.log(x);
						el.style.webkitTransform = `translate3d(0,${y}px,0)`;
						el.style.transform = `translate3d(0,${y}px,0)`;
						let inner = el.getElementsByClassName('inner-hook')[0];
						inner.style.webKitTransform = `translate3d(${x}px,0,0)`;
						inner.style.transform = `translate3d(${x}px,0,0)`;
					}
				}
			},
			droping(el, done) {
				/* eslint-disable no-unused-vars */
				let rf = el.offsetHeight;
				this.$nextTick(() => {
					el.style.webKitTransform = 'translate3d(0,0,0)';
					el.style.transform = 'translate3d(0,0,0)';
					let inner = el.getElementsByClassName('inner-hook')[0];
					inner.style.webKitTransform = 'translate3d(0,0,0)';
					inner.style.transform = 'translate3d(0,0,0)';
					el.addEventListener('transitionend', done);
				});
			},
			dropAfter(el) {
				let ball = this.dropBalls.shift();
				if(ball) {
					ball.show = false;
					el.style.display = 'none';
				}
			}
		},
		components: {
			'cartcontrol': cartcontrol
		}
	};
</script>

<style lang="less">
	@import '../../common/less/z.less';
	@import '../../common/less/css3.less';
	@import '../../common/less/appstyle.less';
	.shopcart {
		.pos(f);
		left: 0;
		bottom: 0;
		z-index: 50;
		width: 100%;
		height: 48px;
		background: #000;
		.content {
			display: flex;
			background: #141d27;
			.content-left {
				flex: 1;
				.logo-wraper {
					.d-ib();
					.pos(r);
					top: -10px;
					margin: 0 12px;
					padding: 6px;
					.wh(56px);
					box-sizing: border-box;
					vertical-align: top;
					.border-radius(90px);
					background: #141d27;
					.logo {
						.wh(100%);
						.tc();
						.border-radius(90px);
						background: #2b343c;
						&.highlight {
							background: cornflowerblue;
						}
						.icon-shopping_cart {
							line-height: 44px;
							.font-s24();
							color: #80858c;
							&.highlight {
								color: #fff;
							}
						}
					}
					.num {
						.pos(a);
						top: 0;
						right: 0;
						width: 24px;
						.l-h(16px);
						.tc();
						.border-radius(16px);
						.font-s9();
						.font-wb();
						color: #fff;
						background-color: rgb(240, 20, 20);
						box-shadow: 0 4px 8px 0 rgba(0, 0, 0, .4);
					}
				}
				.price {
					.d-ib();
					margin-top: 12px;
					vertical-align: top;
					line-height: 24px;
					padding-right: 12px;
					box-sizing: border-box;
					border-right: 1px solid rgba(255, 255, 255, 0.1);
					.font-s16();
					.font-wb();
					color: rgba(255, 255, 255, .4);
					&.highlight {
						color: #fff;
					}
				}
				.desc {
					.d-ib();
					vertical-align: top;
					line-height: 24px;
					margin: 12px 0 0 10px;
					.font-s10();
					color: rgba(255, 255, 255, .4);
				}
			}
			.content-right {
				flex: 0 0 105px;
				width: 105px;
				.pay {
					.l-h(48px);
					.tc();
					.font-s12();
					.font-wb();
					color: rgba(255, 255, 255, .4);
					&.not-enough {
						background: #2b333b;
					}
					&.enough {
						background: #00b43c;
						color: #fff;
					}
				}
			}
		}
		.ball-container {
			.ball {
				.pos(f);
				left: 32px;
				bottom: 22px;
				z-index: 200;
				&.drop-enter-active,
				&.drop-leave-active {
					transition: all 2s cubic-bezier(.49, -0.29, .75, .41);
					.inner {
						.wh(16px);
						.border-radius(90px);
						background: rgb(0, 160, 220);
						&.drop-enter-active,
						&.drop-leave-active {
							transition: all 2s linear;
						}
					}
				}
			}
		}
		.shopcart-list {
			.pos(a);
			bottom: 50px;
			left: 0;
			z-index: -1;
			width: 100%;
			&.fold-enter-active,
			&.fold-leave-active {
				transition: all 0.5s linear;
			}
			&.fold-enter,
			&.fold-leave-active {
				.opa(40);
				transform: translate3d(0, 100%, 0);
			}
			.list-header {
				.l-h(40px);
				padding: 0 18px;
				background: #f3f5f7;
				border-bottom: 1px solid rgba(7, 17, 27, 0.1);
				.title {
					.fl();
					.font-s14();
					color: rgb(7, 17, 27);
				}
				.empty {
					.fr();
					.font-s12();
					color: rgb(0, 160, 220);
				}
			}
			.list-content {
				padding: 0 18px;
				max-height: 217px;
				overflow: hidden;
				background: #fff;
				.food {
					.pos(r);
					padding: 12px 0;
					box-sizing: border-box;
					.border-1px(rgba(7, 17, 27, 0.1));
					.name {
						line-height: 24px;
						.font-s14();
						color: rgb(7, 17, 27);
					}
					.price {
						.pos(a);
						right: 90px;
						bottom: 12px;
						line-height: 24px;
						.font-s14();
						.font-wb();
						color: rgb(240, 20, 20);
					}
					.cartcontrol-wraper {
						.pos(a);
						right: 0;
						bottom: 6px;
					}
				}
			}
		}
		.list-mask {
			.pos(f);
			top: 0;
			left: 0;
			.wh(100%);
			z-index: -2;
			background: rgba(7, 17, 27, 0.6);
			backdrop-filter: blur(10px);
			&.mask-enter-active,
			&.mask-leave-active {
				transition: all 0.5s;
			}
			&.mask-enter,
			&.mask-leave-active {
				transform: translateY(10px);
				.opa(0);
			}
		}
	}
</style>